package net.dreamlu.qa.model;

import java.util.Date;
import java.util.List;

import net.dreamlu.util.TimeUtil;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 问题Model
 * @author lcm
 */
public class Question extends Model<Question> {
	private static final long serialVersionUID = 1L;
	public static final Question dao = new Question();
	
	// 添加问题的统一处理
	public void addQuestion(Question question){
		question.set("asktime", new Date()).set("create_time", new Date()).set("update_time", new Date()).save();
	}
	
	// 更新问题统一处理
	public void updateQuestion(Question question){
		question.set("update_time", new Date()).update();
	}

	// 获取关联的用户
	public User getUser() {
		return User.dao.findById(get("user_id"));
	}
	
	// 获取关联的回答
	public List<Answer> getAnswers (){
		return Answer.dao.find("select * from answer where quest_id = ? and del_status = 0 order by id desc", get("quest_id"));
	}
	
	// 获取最佳回答
	public Answer getBestAnswer(){
		return Answer.dao.findFirst("select * frome answer where quest_id = ? and del_status = 0 and best_answer = 1");
	}
	
	// 获取问题的tags (question_tag 中间表)
	public List<Tags> getTags() {
		String sql = "select t.* from tags t,question_tag qt where qt.question_id=? and qt.del_status =0 and qt.tag_id = t.id order by t.id desc";
		return Tags.dao.find(sql, get("id"));
	}
	
	// 获取问题的分类
	public QuestionSort getQuestionSort(){
		return QuestionSort.dao.findById("sort_id");
	}
	
	// 获取同类问题 (10条最新)
	public List<Question> sortQuestionList() {
		Page<Question> page = Question.dao.paginate(1, 10, "select *", "from question where sort_id = ? and del_status = 0 order by id desc", get("sort_id"));
		return page.getList();
	}
	
	// 获取问题的数目
	public Integer getCount(){
		Long count = Db.queryLong("select count(1) from question where del_status =0");
		return count == null ? 0 : count.intValue();
	}
	
	// 获取首页查询的page分页 question and answer
	public Page<Question> getPage(int pageNum, int pageSize) {
		return Question.dao.paginate(pageNum, pageSize, "select q.*,u.* ", "from question q,user_info u where q.user_id = u.id order by q.id desc");
	}
	
	// 获取热点问题条目
	public Integer getHotCount(){
		Long count = Db.queryLong("select count(1) from question q, answer a where q.del_status = 0 and a.del_status =0 and q.id = a.quest_id");
		return count == null ? 0 : count.intValue();
	}
	
	// 获取首页查询page分页 question and answer 热门
	public Page<Question> getHotPage(int pageNum, int pageSize) {
		return Question.dao.paginate(pageNum, pageSize, "select q.*,a.*,u.* ", "from question q, answer a,user_info u  where q.del_status = 0 and a.del_status =0 and q.user_id = u.id and q.id = a.quest_id order by a.jack_num desc");
	}
	
	// 获取冷门问题条目
	public Integer getColdCount(){
		Long count = Db.queryLong("select count(1) from question q, answer a where q.del_status = 0 and a.del_status =0 and q.id = a.quest_id");
		return count == null ? 0 : count.intValue();
	}
	
	// 获取首页查询page分页 question and answer 无回答
	public Page<Question> getColdPage(int pageNum, int pageSize) {
		return Question.dao.paginate(pageNum, pageSize, "select q.*,u.* ", "from question q,user_info u where q.user_id = u.id and q.id not in (select quest_id from answer where del_status = 0) and q.del_status = 0 order by q.id desc");
	}
	
	// 根据用户名模糊查询 page
	public Page<Question> getLikePage(int pageNum, int pageSize, String like) {
		return Question.dao.paginate(pageNum, pageSize, "select * ", "from question where title like '%"+ like + "%' or content like '%"+ like + "%' and del_status =0 order by id desc");
	} 
	
	// 根据用户名模糊查询 count
	public Integer getLikeCount(String like) {
		Long count = Db.queryLong("select count(1) from question where title like '%"+ like + "%' or content like '%" + like + "%' and del_status =0");
		return count == null ? 0 : count.intValue();
	} 
	
	// 获取 过去了多少时间  
	public String getGoTime(){
		return TimeUtil.getGoTime(getTimestamp("asktime"));
	}
}